package com.eorchis.module.classauditinfo.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.basedao.query.condition.builder.OrderType;
import com.eorchis.core.ui.commond.IQueryCommond;
import com.eorchis.module.classauditinfo.dao.IClassAuditInfoDao;
import com.eorchis.module.classauditinfo.domain.ClassAuditInfo;
import com.eorchis.module.classauditinfo.querybean.ClassAuditInfoQueryBean;
import com.eorchis.module.classauditinfo.ui.commond.ClassAuditInfoQueryCommond;
import com.eorchis.module.classauditinfo.ui.commond.ClassAuditInfoValidCommond;

/**
 * Title: ClassAuditInfoDaoImpl<br>
 * Description: 班级审核信息数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2013-12-31
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.classauditinfo.dao.impl.ClassAuditInfoDaoImpl")
public class ClassAuditInfoDaoImpl
					extends HibernateAbstractBaseDao
					implements IClassAuditInfoDao {
	
	public Class<? extends IBaseEntity> entityClass() {
		return ClassAuditInfo.class;
	}
	
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		ClassAuditInfoQueryCommond classAuditInfoQueryCommond = (ClassAuditInfoQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM ClassAuditInfo t");
		defaultQueryConditionBuilder.addCondition("t.auditId", CompareType.IN, classAuditInfoQueryCommond.getSearchAuditIds());
		defaultQueryConditionBuilder.addCondition("t.auditId", CompareType.EQUAL,classAuditInfoQueryCommond.getSearchAuditId());
		defaultQueryConditionBuilder.addCondition("t.trainingClass.classId", CompareType.EQUAL,classAuditInfoQueryCommond.getSearchClassId());
		defaultQueryConditionBuilder.addSort("t.createDate", OrderType.DESC);
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
	}
	
	/*
	 * 添加审核信息
	 */
	@Override
	public void addAuditInfos(List<ClassAuditInfoValidCommond> list) throws Exception {
		Session session=null;
		try {
			session=this.getDataAccessObject();
			int j=0;
			if(list!=null&&list.size()>0) {
				for(int i=0;i<list.size();i++) {
					ClassAuditInfoValidCommond vCommond=list.get(i);
					ClassAuditInfo classAuditInfo=(ClassAuditInfo) vCommond.toEntity();
					session.save(classAuditInfo);
					j++;
					// 批插入的对象立即写入数据库并释放内存  每插入20条数据进行内存释放
					if(j % 20 == 0) {
						 session.flush();  
	                     session.clear();  
					}
				}
//				session.getTransaction().commit();
//				t.commit();
			}
		}catch (Exception e) {
//			session.getTransaction().rollback();
			//t.rollback();
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询班级撤回原因
	 * @param 
	 * @throws Exception
	 * @author Max
	 * @date 2014-12-17
	 */
	@Override
	public  Map<String,String> getAuditReson(ClassAuditInfoQueryCommond qCommond)throws Exception {
		
		String sql="select cai.CLASS_ID as classId, max(cai.CREATE_DATE) as createDate,max(cai.audit_id) as auditId";
			   sql+=" from CLASS_AUDIT_INFO cai where cai.CLASS_ID in(:ids) group by cai.CLASS_ID ";
			   sql+=" order by max(cai.CREATE_DATE) desc";
			   Map<String, Object> params=new HashMap<String, Object>();
			   params.put("ids", qCommond.getSearchClassIds());
			   List<Object> list=(List<Object>) this.executeFind(QueryStringType.SQL, sql, params);
			   
			   
			   Map<String,String> result=new HashMap<String,String>();
			   if(list!=null&&list.size()>0) {
				   String [] ids=new String [list.size()];
				   for(int i=0;i<list.size();i++) {
					   Object [] values=(Object[]) list.get(i);
					   ids[i]=values[2].toString();
				   }
				   
				   Map<String, Object> params2=new HashMap<String, Object>();
				   String  sql2="select c.CLASS_ID as classId,c. AUDIT_REASON as auditReason from CLASS_AUDIT_INFO c where c.audit_id in(:ids2)";
				   
				   params2.put("ids2", ids);
				   List<Object> list2=(List<Object>) this.executeFind(QueryStringType.SQL, sql2, params2);
				   if(list2!=null&&list2.size()>0) {
					   for(Object o:list2) {
						   Object [] values=(Object[]) o;
						   result.put(values[0]+"", values[1]+"");
					   }
				   }
			   }
			   
			   return result;
	}
	/**
	 * 查询某个班，班级撤回原因
	 * @param 
	 * @throws Exception
	 * @author Max
	 * @date 2014-12-17
	 */
	@Override
	public ClassAuditInfoQueryBean getAuditInfoByClassId(
			ClassAuditInfoValidCommond qCommond) throws Exception {
		ClassAuditInfoQueryBean cai=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" select cai.CLASS_ID as \"classId\", cai.CREATE_DATE as \"createDate\",cai.audit_reason as \"auditReson\" from CLASS_AUDIT_INFO cai where cai.CLASS_ID =:classId ");
		sql.append(" and cai.aduit_state = 2 order by cai.CREATE_DATE desc");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("classId", qCommond.getClassId());
		List<Object> obj=(List<Object>) this.executeFind(QueryStringType.SQL, sql.toString(), params);
		for(int i =0;i<obj.size();i++){
			cai=new ClassAuditInfoQueryBean();
			Object [] o =(Object[]) obj.get(0);
			cai.setClassId(o[0].toString());
			cai.setAuditReson(o[2].toString());
			cai.setCreateDate((Date) o[1]);
		}
		return cai;
	}
	
}
